var element, form;

$(function () {
    // 检查是否登录
    if (!tokenUtil.getToken()) {
        location.href = 'login.html';
        return;
    }
    // 初始化
    initLayUI(function () {
        initMenu(function () {
            $(".route").first().click();
            $("#ifm").attr("src", $(".route").first().attr("href"));
        });
        initUserInfo();
    });
});

function initLayUI(cb) {
    layui.use(['element', 'layer'], function () {
        element = layui.element, form = layui.form;

        element.on('nav(home)', function (elem) {
            var citeLength = $("#cite").length;
            var text = $(elem[0]).text();

            if (text.indexOf("首页") !== -1) {
                $(".add-breadcrumb-span").remove();
                $(".add-cite").remove();
            } else {
                if (citeLength === 0) {
                    $("#breadcrumb").append("<span class='layui-my-breadcrumb-span add-breadcrumb-span'>/</span><a><cite class='add-cite' id=\"cite\">" + text + "</cite></a>")
                } else {
                    $("#cite").text(text);
                }
            }
        });

        cb();
    });
}

function initMenu(cb) {
    ajaxUtil.post(null, "/user/queryUserMenus", function (res) {
        if (res.code === 200) {
            var data = res.data;
            var ul = ' <ul class="layui-nav layui-nav-tree layui-inline" lay-filter="home">';
            for (var i = 0; i < data.length; i++) {
                ul += ' <li class="layui-nav-item">';
                if (data[i].children === null) {
                    ul += ' <a href="' + data[i].routePath + '" class="route" target="ifm"><i class="' + data[i].style + '"></i> <cite>' + data[i].name + '</cite></a>';
                } else {
                    ul += '<a href="javascript:;"><i class="' + data[i].style + '"></i> <cite>' + data[i].name + '</cite></a>';
                    ul += "<dl class='layui-nav-child'>";
                    for (var j = 0; j < data[i].children.length; j++) {
                        ul += "<dd>";
                        ul += "<a href='" + data[i].children[j].routePath + "' target='ifm'>" + '     ' + data[i].children[j].name + "</a>";
                        ul += "</dd>";
                    }
                    ul += "</dl>";
                }
                ul += "</li>";
            }
            ul += "</ul>";
            $("#nav").html(ul);
            element.init();

            cb();
        } else {
            layerUtil.openMsgError('查询菜单遇到错误');
        }
    }, function () {
        layerUtil.openMsgError("查询菜单失败，请稍后再试");
    });
}

function initUserInfo() {
    let currentUser = getTempData('userInfo');
    $("#mySetting").text("用户：" + currentUser.userName);
}

function returnHome() {
    $(".route").first().click();
    $("#ifm").attr("src", $(".route").first().attr("href"));
}

function loginOut() {
    tokenUtil.setToken('');
    location.href = 'login.html';
}

function resetPassword() {
    $("#resetPasswordBtn").click();
    $("#ifm").attr("src", "views/system/password.html")
}